#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-10-30 9:10 上午
# @Author  : yangdy
# @File    : 2020-10-30.463.岛屿的周长
# @Software: PyCharmCE
from tools.my_methods import *


class Solution:
    def islandPerimeter(self, grid: List[List[int]]) -> int:
        ans = 0
        if not grid:
            return ans
        for i in range(len(grid)):
            for j in range(len(grid[i])):
                if grid[i][j] == 1:
                    if i == 0:
                        ans += 1
                    else:
                        ii, jj = i-1, j
                        if grid[ii][jj] == 0:
                            ans += 1
                    if j == len(grid[i]) - 1:
                        ans += 1
                    else:
                        ii, jj = i, j+1
                        if grid[ii][jj] == 0:
                            ans += 1
                    if i == len(grid) - 1:
                        ans += 1
                    else:
                        ii, jj = i+1, j
                        if grid[ii][jj] == 0:
                            ans += 1
                    if j == 0:
                        ans += 1
                    else:
                        ii, jj = i, j-1
                        if grid[ii][jj] == 0:
                            ans += 1
        return ans
